var express = require('express');
var axios = require("axios");
var router = express.Router();

/* GET home page. */
router.get('/', (req, res, next) => {
  res.render('index', { title: 'Express' });
});

router.use('/raw', async (req, res, next) => {
  try {
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild'); 
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); 
    res.header('Cache-Control', 'no-cache, no-store, must-revalidate'); 
    res.header('Pragma', 'no-cache'); 
    res.header('Expires', '0');

    var options = {
      method: req.method.toUpperCase(),
      url: req.query.url,
      hostname: 'api.chuchujie.com',
      port: 80, //端口号 https默认端口 443， http默认的端口号是80
      headers: {
        "Connection": "keep-alive",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
      }
    };

    console.log(req.method.toUpperCase(), options.url)

    var response = await axios(options);
    console.log(response)

    res.header('Content-Type', response.headers["content-type"]);
    res.send(response.data); 
    } catch(err) {
      console.log(err)
    }
});

module.exports = router;
